package com.mf.web.authority;

import com.mf.common.sso.LoginContext;
import com.mf.domain.authority.RoleResourceRelation;
import com.mf.domain.general.PageBean;
import com.mf.domain.general.tree.ZTree;
import com.mf.service.authority.RoleResourceRelationService;
import com.mf.web.framework.advice.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色资源关联信息Controller
 * User: zhaoming
 * DateTime: 2016-07-15
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/web/roleResourceRelation")
public class RoleResourceRelationController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private RoleResourceRelationService roleResourceRelationService;

    /**
     * 初始化页面
     * @param request 请求对象
     * @param response 响应对象
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("role_resource_relation_index");
        return modelAndView;
    }

    /**
     * 新增页面
     * @param request 请求对象
     * @param response 响应对象
     * @return 模型视图
     */
    @RequestMapping(value = "/insertPage", method = RequestMethod.GET)
    public ModelAndView insertPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("role_resource_relation_add");
        return modelAndView;
    }

    /**
     * 新增角色资源关联信息
     * @param roleResourceRelation 角色资源关联信息Domain
     * @param resources 资源字符串
     * @return 执行结果
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean insert(RoleResourceRelation roleResourceRelation, String resources) {
        boolean flag = roleResourceRelationService.insert(roleResourceRelation, resources);
        if(!flag){
            throw new BusinessException("新增失败");
        }
        return flag;
    }

    /**
     * 维护页面
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 模型视图
     */
    @RequestMapping(value = "/updatePage", method = RequestMethod.POST)
    public ModelAndView updatePage(RoleResourceRelation roleResourceRelation) {
        ModelAndView modelAndView = new ModelAndView("role_resource_relation_edit");
        modelAndView.addObject("roleResourceRelation", roleResourceRelationService.selectByPrimaryKey(roleResourceRelation));
        return modelAndView;
    }

    /**
     * 根据主键维护角色资源关联信息
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean updateByPrimaryKey(RoleResourceRelation roleResourceRelation) {
        boolean flag = roleResourceRelationService.updateByPrimaryKey(roleResourceRelation, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("维护失败");
        }
        return flag;
    }

    /**
     * 根据主键删除角色资源关联信息 (物理删除)
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean deleteByPrimaryKey(RoleResourceRelation roleResourceRelation){
        boolean flag = roleResourceRelationService.deleteByPrimaryKey(roleResourceRelation);
        if(!flag){
            throw new BusinessException("删除角色资源关联信息失败");
        }
        return flag;
    }

    /**
     * 根据主键删除角色资源关联信息 (逻辑删除)
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteByPrimaryKeyLogically", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean deleteByPrimaryKeyLogically(RoleResourceRelation roleResourceRelation){
        boolean flag = roleResourceRelationService.deleteByPrimaryKeyLogically(roleResourceRelation, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("删除角色资源关联信息失败");
        }
        return flag;
    }

    /**
     * 根据主键查询角色资源关联信息
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 角色资源关联信息
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public RoleResourceRelation selectByPrimaryKey(RoleResourceRelation roleResourceRelation) {
        return roleResourceRelationService.selectByPrimaryKey(roleResourceRelation);
    }

    /**
     * 根据条件查询角色资源关联信息
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 角色资源关联信息列表
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<RoleResourceRelation> selectByCondition(RoleResourceRelation roleResourceRelation) {
        return roleResourceRelationService.selectByCondition(roleResourceRelation);
    }

    /**
     * 分页查询角色资源关联信息
     * @param roleResourceRelation 角色资源关联信息Domain
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 角色资源关联信息列表
     */
    @RequestMapping(value = "/pageQuery", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> pageQuery(RoleResourceRelation roleResourceRelation,
                                    @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                    @RequestParam(value = "rows", required = false, defaultValue = "10") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        PageBean<RoleResourceRelation> pageBean = new PageBean<>(page, pageSize);
        LinkedHashMap<String, String> sortItemMap = new LinkedHashMap<>();
        sortItemMap.put(sortName, sortOrder);
        pageBean.setSortItemMap(sortItemMap);
        pageBean = roleResourceRelationService.pageQuery(pageBean, roleResourceRelation);

        Map<String, Object> result = new HashMap<>();
        result.put("root", pageBean.getUnderly());
        result.put("page", pageBean.getPage());
        result.put("total", pageBean.getPageCount());
        result.put("records", pageBean.getItemCount());
        return result;
    }

    /**
     * 获取资源树
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 资源信息列表
     */
    @RequestMapping(value = "/getResourceTree", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<ZTree> getResourceTree(RoleResourceRelation roleResourceRelation) {
        return roleResourceRelationService.getResourceTree(roleResourceRelation);
    }

}